package gdou.gym.service.user.impl;

import com.github.pagehelper.PageHelper;
import gdou.gym.dao.DiscreditMapper;
import gdou.gym.dao.UserMapper;
import gdou.gym.entity.Discredit;
import gdou.gym.entity.DiscreditExample;
import gdou.gym.entity.User;
import gdou.gym.entity.UserExample;
import gdou.gym.exception.StateCode;
import gdou.gym.exception.SysException;
import gdou.gym.service.user.UserService;
import gdou.gym.sys.log.Log;
import gdou.gym.util.MD5Util;
import gdou.gym.vo.MyPageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private DiscreditMapper discreditMapper;

    @Override
    public User findAdminByAccount(String userID) {
        return userMapper.selectByPrimaryKey(userID);

    }

    @Override
    public List<User> queryByCondition(MyPageInfo<User> info) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        User userInfo = info.getQueryInfo();
        if (!"".equals(userInfo.getAccount())) {
            criteria.andAccountLike(userInfo.getAccount() + "%");
        }
        if (userInfo.getState() != null) {
            criteria.andStateEqualTo(userInfo.getState());
        }
        if (userInfo.getType() != null) {
            criteria.andTypeEqualTo(userInfo.getType());
        }
        PageHelper.startPage(info.getPageNo(),info.getPageSize());
        return userMapper.selectByExample(userExample);
    }

    @Override
    public String getDiscreditReason(String account) {
        String reason = discreditMapper.getReasonByAccount(account);
        if (reason == null || "".equals(reason.trim())) {
            throw new SysException(StateCode.NoDiscover_ERROR);
        }
        return reason;
    }

    @Override
    @Transactional
    @Log
    public void upDataUser(String account, String reason) {
        User user = new User();
        user.setAccount(account);
        UserExample userExample = new UserExample();
        userExample.createCriteria().andAccountEqualTo(account);
        DiscreditExample discreditExample = new DiscreditExample();
        discreditExample.createCriteria().andAccountEqualTo(account);
        /*说明信用表移除  用户表没更改，行用表删除*/
        if ("".equals(reason.trim())) {
            user.setState(false);
            discreditMapper.deleteByExample(discreditExample);
            userMapper.updateByExampleSelective(user, userExample);
            return;
        }
        /*
         * 可能是更新原因，也可能是新增,先查询封禁表是否有人
         * */
        List<Discredit> discredits = discreditMapper.selectByExample(discreditExample);
        /*更新原因 */
        if (!discredits.isEmpty()) {
            Discredit discredit = new Discredit();
            discredit.setReason(reason);
            discredit.setAccount(account);
            discreditMapper.updateByExampleSelective(discredit, discreditExample);
            return;
        }
        /*新增   */
        Discredit discredit = new Discredit();
        discredit.setReason(reason);
        discredit.setAccount(account);
        discreditMapper.insertSelective(discredit);
        user.setState(true);
        userMapper.updateByExampleSelective(user, userExample);
    }

    @Override
    public User login(User u) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andPswEqualTo(MD5Util.getMD5(u.getPsw())).andAccountEqualTo(u.getAccount());
        List<User> users = userMapper.selectByExample(userExample);
        if (users.size() != 1) {
            throw new SysException(StateCode.USER_AccountOrPswERROR);
        }
        if (users.get(0).getState()) {
            throw new SysException(StateCode.UserAccountState_Error);
        }
        return users.get(0);
    }

    @Override
    @Log
    public void register(User user) {
        User user1 = userMapper.selectByPrimaryKey(user.getAccount());
        if (user1 == null) {
            user.setPsw(MD5Util.getMD5(user.getPsw()));
            user.setState(false);
            user.setType(false);
            userMapper.insert(user);
            return;
        }
        throw new SysException(StateCode.UserAccount_Error);
    }

    public void test(String a, Integer b) {
        System.out.println("service=" + a);
        throw new RuntimeException("测试异常");
    }
}
